package com.wwwarehouse.usercenter.fragment.business_relationship;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileDetailBean;
import com.wwwarehouse.usercenter.bean.business_relationship.GetRelationSalesmansBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by xiaoke.ni on 2017/10/24.
 */

public class BRCustomerFileCheckDetailFragment extends BaseFragment implements View.OnClickListener {

    private View mView;

    private StateLayout mStateLayout;
    private ImageView mIconIv;
    private TextView mCompanyTv;
    private TextView mAdversePhoneTv;
    private TextView mAdverseAddressTv;
    private TextView mAdverseSalesmanTv;
    private TextView mCardLabel1Tv;
    private TextView mCardLabel2Tv;
    private TextView mCardLabel3Tv;
    private TextView mCardLabel4Tv;
    private TextView mCardLabel5Tv;
    private TextView mCardLabel6Tv;
    private TextView mCardLabelTv;
    private TextView mCardPeopleTv;
    private TextView mCardRemarkTv;
    private RelativeLayout mCardLabelRl;
    private RelativeLayout mCardLmCardPeopleRl;
    private RelativeLayout mCardRemarkRl;


    private BRCustomerFileDetailBean bean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getActivity(), R.layout.fragment_br_customer_file_detail, null);
        return mView;
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.card_label_rl) {
            ArrayList<BRCustomerFileDetailBean.TagsBean> tagsBeenList = bean.getTags();
            BRCFCDTagsFragment fragment = new BRCFCDTagsFragment();
            Bundle bundle = new Bundle();
            bundle.putString("tagBusinessUkid", getArguments().getString("buId"));
            bundle.putParcelableArrayList("tags", tagsBeenList);
            fragment.setArguments(bundle);
            pushFragment(fragment);
        } else if (id == R.id.card_people_rl) {
            BRCFCDSelectSalesmanFragment brcfdSelectSalesmanFragment = new BRCFCDSelectSalesmanFragment();
            Bundle bundle = new Bundle();
            bundle.putString("buId", getArguments().getString("buId"));
            bundle.putString("relationUkid", getArguments().getString("relationUkid"));
            brcfdSelectSalesmanFragment.setArguments(bundle);
            pushFragment(brcfdSelectSalesmanFragment);
        } else if (id == R.id.card_remark_rl) {
            BRCFCDRemarkFragment brcfdRemarkFragment = new BRCFCDRemarkFragment();
            Bundle bundle = new Bundle();
            bundle.putString("remark", bean.getRemark());
            brcfdRemarkFragment.setArguments(bundle);
            pushFragment(brcfdRemarkFragment);
        }

    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BRCustomerFileCheckDetailFragment) {
            String relationType = getArguments().getString("relationType");
            if (relationType.equals("CUSTOMER")) {
                mActivity.setTitle(getResources().getString(R.string.br_customer_file_maintain));
            } else if (relationType.equals("SUPPLIER")) {
                mActivity.setTitle(getResources().getString(R.string.br_supplier_file_maintain));
            }
        }
    }

    @Override
    public void requestDatas() {
        initView();
        getRelationDetailedInfo();
    }

    private void initView() {
        mStateLayout = (StateLayout) mView.findViewById(R.id.slt);
        mIconIv = (ImageView) mView.findViewById(R.id.icon_iv);
        mCompanyTv = (TextView) mView.findViewById(R.id.company_tv);
        mAdversePhoneTv = (TextView) mView.findViewById(R.id.adverse_phone_tv);
        mAdverseAddressTv = (TextView) mView.findViewById(R.id.adverse_address_tv);
        mAdverseSalesmanTv = (TextView) mView.findViewById(R.id.adverse_salesman_tv);
        mCardLabel1Tv = (TextView) mView.findViewById(R.id.card_label1_tv);
        mCardLabel2Tv = (TextView) mView.findViewById(R.id.card_label2_tv);
        mCardLabel3Tv = (TextView) mView.findViewById(R.id.card_label3_tv);
        mCardLabel4Tv = (TextView) mView.findViewById(R.id.card_label4_tv);
        mCardLabel5Tv = (TextView) mView.findViewById(R.id.card_label5_tv);
        mCardLabel6Tv = (TextView) mView.findViewById(R.id.card_label6_tv);
        mCardLabelTv = (TextView) mView.findViewById(R.id.card_label_tv);
        mCardPeopleTv = (TextView) mView.findViewById(R.id.card_people_tv);
        mCardRemarkTv = (TextView) mView.findViewById(R.id.card_remark_tv);
        mCardLabelRl = (RelativeLayout) mView.findViewById(R.id.card_label_rl);
        mCardLmCardPeopleRl = (RelativeLayout) mView.findViewById(R.id.card_people_rl);
        mCardRemarkRl = (RelativeLayout) mView.findViewById(R.id.card_remark_rl);

        mCardLmCardPeopleRl.setOnClickListener(this);
    }

    /**
     * 客户档案详情
     */
    private void getRelationDetailedInfo() {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("buId", getArguments().getString("buId"));
        hashMap.put("relationUkid", getArguments().getString("relationUkid"));
        NoHttpUtils.httpPost(UserCenterConstant.GET_RELATION_DETAILED_INFO, hashMap, onResponseListener, 0);
        mStateLayout.showProgressView(true);
    }


    private NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        mStateLayout.showContentView();
                        bean = JSON.parseObject(commonClass.getData().toString(), BRCustomerFileDetailBean.class);
                        notifyData();
                    } else {
                        mStateLayout.showEmptyView(commonClass.getMsg(), true);
                        mStateLayout.setEmptyListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                getRelationDetailedInfo();
                            }
                        });
                        ToastUtils.showToast(commonClass.getMsg());
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };


    private void notifyData() {
        ImageloaderUtils.displayCircleImage(bean.getBuLogoUrl(), mIconIv);
        mCompanyTv.setText(bean.getBuName());
        mAdversePhoneTv.setText(getString(R.string.user_br_contact) + bean.getBuContact());
        String address = "";
        if (!TextUtils.isEmpty(bean.getBuProvince()))
            address += bean.getBuProvince();
        if (!TextUtils.isEmpty(bean.getBuCity()))
            address += bean.getBuCity();
        if (!TextUtils.isEmpty(bean.getBuArea()))
            address += bean.getBuArea();
        if (!TextUtils.isEmpty(bean.getBuStreet()))
            address += bean.getBuStreet();
        if (!TextUtils.isEmpty(bean.getBuAddress()))
            address += bean.getBuAddress();
        mAdverseAddressTv.setText(getString(R.string.user_br_address) + address);
        String adverseSalesman = "";
        for (BRCustomerFileDetailBean.OtherSalesManNamesBean salesMansBean : bean.getOtherSalesManNames()) {
            if (!adverseSalesman.equals(""))
                adverseSalesman += "、";
            adverseSalesman += salesMansBean.getSalesManName();
        }
        mAdverseSalesmanTv.setText(getString(R.string.user_br_adverse_salesman) + adverseSalesman);

        List<BRCustomerFileDetailBean.TagsBean> tagsBeanList = bean.getTags();
        if (tagsBeanList.size() > 0)
            mCardLabelRl.setOnClickListener(this);
        mCardLabelTv.setVisibility(View.VISIBLE);
        for (int i = 0; i < tagsBeanList.size(); i++) {
            mCardLabelTv.setVisibility(View.GONE);
            if (i == 0) {
                mCardLabel1Tv.setText(tagsBeanList.get(i).getTagName());
                mCardLabel1Tv.setVisibility(View.VISIBLE);
            }
            if (i == 1) {
                mCardLabel2Tv.setText(tagsBeanList.get(i).getTagName());
                mCardLabel2Tv.setVisibility(View.VISIBLE);
            }
            if (i == 2) {
                mCardLabel3Tv.setText(tagsBeanList.get(i).getTagName());
                mCardLabel3Tv.setVisibility(View.VISIBLE);
            }
            if (i == 3) {
                mCardLabel4Tv.setText(tagsBeanList.get(i).getTagName());
                mCardLabel4Tv.setVisibility(View.VISIBLE);
            }
            if (i == 4) {
                mCardLabel5Tv.setText(tagsBeanList.get(i).getTagName());
                mCardLabel5Tv.setVisibility(View.VISIBLE);
            }
            if (i == 5) {
                mCardLabel6Tv.setText(tagsBeanList.get(i).getTagName());
                mCardLabel6Tv.setVisibility(View.VISIBLE);
            }
            if (i > 5) {
                mCardLabel6Tv.setText("……");
                break;
            }
        }
        if (bean.getSalesMans().size() > 2) {
            mCardPeopleTv.setText(bean.getSalesMans().size() + getString(R.string.user_br_count_salesman));
        } else {
            String salesman = "";
            for (BRCustomerFileDetailBean.SalesMansBean salesMansBean : bean.getSalesMans()) {
                if (!salesman.equals(""))
                    salesman += "、";
                salesman += salesMansBean.getSalesManName();
            }
            mCardPeopleTv.setText(salesman);
        }
        if (!TextUtils.isEmpty(bean.getRemark())) {
            mCardRemarkRl.setOnClickListener(this);
            mCardRemarkTv.setText(bean.getRemark());
        }
    }

}
